<?php

namespace App\Admin\Controllers;

use App\Models\CardaddModel;
use App\Models\CardsModel;
use Illuminate\Http\Request;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\Facades\DB;

class CardaddController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = 'CardaddModel';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new CardaddModel());

        $grid->column('id', __('Id'));
        $grid->column('product_name', __('产品名'))->display(function ($product){
            $product=$this->product;
            return $product->p_name;
        });
        $grid->column('return_time', __('有效时间'));
        $grid->column('count', __('生成卡劵数'));

        $grid->column('created_at', __('生成时间'));
        $grid->column('updated_at', __('上次修改时间'));


        $grid->column('takecard', __('添加'))->display(function (){
            $id=$this->id;
            $href='addcards?id='.$id;
            return "<a href=$href>执行添加</a>";
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(CardaddModel::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('product_name', __('Product name'));
        $show->field('return_time', __('Return time'));
        $show->field('count', __('Count'));

        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $data=DB::table('products')->get();
        $array=array($data);

        $form = new Form(new CardaddModel());
        $form->select('product_name', __('产品名....产品名'))->options(CardaddModel::getProduncts());
        $form->number('return_time', __('Return time'));
        $form->number('count', __('Count'));

        return $form;
    }
    public function AddCards(Request $request)
    {
        //date_default_timezone_set("PRC");
        $id=$request['id'];
        //这里是获取表中数据，//
        $data=DB::table('cardadd')->where('id',$id)->first();
        //获取商品的代号id
        $product_id= $data -> product_name;
        //获取代号id的商品名称
        $data2=DB::table('products')->where('id',$product_id)->first();
        $name=$data2->p_name;
        $count= $data -> count;
        $day= $data -> return_time;
        //礼品卷采用商品名拼音首字母，及拼接生成
        $len1=mb_strlen($name);
        //mb_strlen( $str, $encoding )
        $prefix="";
        for ($i=0;$i<$len1;$i++){
            $pat5='/^[\x{4e00}-\x{9fa5}]{1,5}$/u';
            if (ctype_alnum(mb_substr($name, $i, 1))){
                $prefix.=mb_substr($name, $i, 1);
            }elseif (preg_match($pat5,mb_substr($name, $i, 1))==1){
                $prefix.=$this->getFirstCharter($tcChar = mb_substr($name, $i, 1));
            }
        }
        $cards=[];
        $number=10000000;
        $str="qwertyuiopasdfghjklzxcvbnm";
        $len=strlen($str);
        for ($i=0; $i<$count;$i++){
            $pwd='';
            for($j=0;$j<8;$j++){
                $pwd.=$str[rand(0,$len-1)];
            }
            $account=$prefix.($number+$i);
            $cardsdata_rand=DB::table('cards')->where('account',$account)->first();
            if ($cardsdata_rand!=null){
                $cardsdata=DB::table('cards')->orderBy('id', 'desc')->first();
                $card_account=$cardsdata->account;
                $account_lenght=strlen($card_account);
                $prefixlenght=$account_lenght-8;
                $account_prefix=substr($card_account,0,$prefixlenght);
                $account_rprefix=substr($card_account,-8)+1+$i;
                $cards[] = [
                    'account'=>$account_prefix.$account_rprefix,
                    'password'=>$pwd,
                    'status'=>1,
                    'p_id'=>$product_id,
                    'created_at'=>time(),
                    'expiration_time'=>time()+$day*24*60*60,
                ];
            }else{
                $cards[] = [
                    'account'=>$prefix.($number+$i),
                    'password'=>$pwd,
                    'status'=>1,
                    'p_id'=>$product_id,
                    'created_at'=>time(),
                    'expiration_time'=>time()+$day*24*60*60,
                ];
            }
        }
        $res=CardsModel::AddCards($cards);
        if ($res){
            echo "<script>alert('生成成功');location.href='cards';</script>";
        }
    }

    //php获取中文字符拼音首字母
    function getFirstCharter($str){
        if(empty($str)){return '';}
        $fchar=ord($str{0});
        if($fchar>=ord('A')&&$fchar<=ord('z')) return strtoupper($str{0});
        $s1=iconv('UTF-8','gb2312',$str);
        $s2=iconv('gb2312','UTF-8',$s1);
        $s=$s2==$str?$s1:$str;
        $asc=ord($s{0})*256+ord($s{1})-65536;
        if($asc>=-20319&&$asc<=-20284) return 'A';
        if($asc>=-20283&&$asc<=-19776) return 'B';
        if($asc>=-19775&&$asc<=-19219) return 'C';
        if($asc>=-19218&&$asc<=-18711) return 'D';
        if($asc>=-18710&&$asc<=-18527) return 'E';
        if($asc>=-18526&&$asc<=-18240) return 'F';
        if($asc>=-18239&&$asc<=-17923) return 'G';
        if($asc>=-17922&&$asc<=-17418) return 'H';
        if($asc>=-17417&&$asc<=-16475) return 'J';
        if($asc>=-16474&&$asc<=-16213) return 'K';
        if($asc>=-16212&&$asc<=-15641) return 'L';
        if($asc>=-15640&&$asc<=-15166) return 'M';
        if($asc>=-15165&&$asc<=-14923) return 'N';
        if($asc>=-14922&&$asc<=-14915) return 'O';
        if($asc>=-14914&&$asc<=-14631) return 'P';
        if($asc>=-14630&&$asc<=-14150) return 'Q';
        if($asc>=-14149&&$asc<=-14091) return 'R';
        if($asc>=-14090&&$asc<=-13319) return 'S';
        if($asc>=-13318&&$asc<=-12839) return 'T';
        if($asc>=-12838&&$asc<=-12557) return 'W';
        if($asc>=-12556&&$asc<=-11848) return 'X';
        if($asc>=-11847&&$asc<=-11056) return 'Y';
        if($asc>=-11055&&$asc<=-10247) return 'Z';
        return null;
    }
}
